<?php

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取总页面数
// 	$wjcontent = mb_convert_encoding(file_get_contents("http://www.nonadm.com/list/zt/wj_1.htm"),"UTF-8","GB2312");
	$lzcontent = mb_convert_encoding(file_get_contents("http://www.xindm.cn/list03.asp?page="),"UTF-8","GB2312");
	
// 	$wjc = explode('共有<b>',$wjcontent);
	$lzc = explode('共有<b>',$lzcontent);
// 	$wjc = $wjc[1];
	$lzc = $lzc[1];
	$lzc = explode('<',$lzc);
// 	$wjc = explode('<',$wjc);
// 	$twjc = $wjc[0];
	$tlzc = $lzc[0];
// 	unset($wjc);
	unset($lzc);
// 	unset($wjcontent);
	unset($lzcontent);
	//开始整站采集
	//定义系统预置URL
	//完结的
// 	$pre_wj_url = "http://www.nonadm.com/list/zt/wj_";
	$pre_lz_url = "http://www.xindm.cn/list03.asp?page=";
// 	$end_flg = '.htm';
	$end_flg = '';
	if($spc == 'wj'){
// 		getwj($twjc,$pre_wj_url,$end_flg);
	}else if($spc == 'lz'){
		getlz($tlzc,$pre_lz_url,$end_flg);
	}else if($spc == 'all'){
		getlz($tlzc,$pre_lz_url,$end_flg);
// 		getwj($twjc,$pre_wj_url,$end_flg);
	}
	
	echo "采集完毕";
	
	ob_end_flush();
	
	
	//以下为函数定义
	
	
	
	function getwj($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i < $count ;$i++){
			$content = mb_convert_encoding(file_get_contents($pre . $i . $end),"UTF-8","GB2312");
			$content = explode('<DIV class=lastnew02 id=lastnew01 style="DISPLAY: none">',$content);
			$content = explode('<div class="lastnew02" id="lastnew02"> ',$content[1]);
			$content = $content[0];
			$sstr = array('<UL class=cmimg_lastimg02><LI><A class=cmimg02 ','target=_blank','</A> </LI><LI><H2><A title=');
			$rstr = array("\r\n","\r\n","\r\n");
			$content = str_replace($sstr,$rstr,$content);
			
			//直接获取连接
			$match_str = "/title=(.*) href=\"(.*)\"/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[2];
			foreach($urln as $k=>$v){
				$thisurl = "http://www.nonadm.com" . $v . "/";
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				
				//continue;
				

				getsig($thisurl);
			}
		}
		unset($content);
		
	}
	
	function getlz($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i <= $count ;$i++){
			$content = mb_convert_encoding(file_get_contents($pre . $i . $end),"UTF-8","GB2312");
			$content = explode("		\r\n<tr align='center'><td width='16%' valign='top'>",$content);
			$content = explode('</span></TD></TR></table></td></tr></table></td></tr>		',$content[1]);
			$content = $content[0];
// 			$sstr = array('<UL class=cmimg_lastimg02><LI><A class=cmimg02 ','target=_blank','</A> </LI><LI><H2><A title=');
// 			$rstr = array("\r\n","\r\n","\r\n");
// 			$content = str_replace($sstr,$rstr,$content);
			
			//直接获取连接
			$match_str = "/<a href=\'type.asp\?typeid=(.*)\'/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[1];
			$urln = array_flip(array_flip($urln));
			sort($urln);
			foreach($urln as $k=>$v){
				$thisurl = "http://mh.xindm.cn/type.asp?typeid=" . $v;
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				
				getsig($thisurl,$v);
			}
		}
		unset($content);
	}
	
	function getsig($url,$tid){
		global $DB;
		$id = $tid;
		$url_pre = $url;
		$url = 'http://mh.xindm.cn/type.asp?typeid=' . $tid;
		$pic_url_pre = 'http://mh.xindm.cn/book';
		$pic_file_pre = 'xindm_cn_';
			
		$content = 	file_get_contents($url);
		$content = 	mb_convert_encoding($content,"UTF-8","GB2312");
		
		//获取名称
		$name = explode('>> ',$content);
		$name = $name[1];
		$name = explode('</title>',$name);
		$name = $name[0];
		$name = explode('[',$name);
		$name = $name[0];
		
		
		//获取最后更新时间
		$dt = explode('更新时间：',$content);
		$dt = $dt[1];
		$dt = explode('</span>',$dt);
		$dt = strtotime(trim($dt[0]));
		
		//获取URL列表
		$urls = explode('<table width="100%" border="0" cellspacing="0" cellpadding="9">',$content);
		$urls = $urls[1];
		$urls = explode('</table>',$urls);
		$urls = $urls[0];
		$match_str = "/a>  <a href=\'(.*)\' target=\'\_blank\'><font/isU";
		preg_match_all($match_str,$urls,$urln);
		$urln = $urln[1];
		$num = count($urln);
			$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'xindmtags','where'	=> "1 " . $where ,)
			);	
			if ($result['max'] > 0) {
				
			}
			else{
				do_taginsert($id,$dt,$name,0);
			}
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => 'xindmtags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		$urln = array_reverse($urln);
		foreach ($urln as $k=>$v) {
			if ($k < $dbcount) {
				continue;
			}
			$current_url = $urln[$k];
			$s_pic_content = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
			$res = get_comic_pics($s_pic_content,$pic_url_pre,$pic_file_pre,$current_url);
			//这里添加数据库插入语句
			foreach ($res as $v) {
				//直接桉页插入
				do_photoinsert($id,$v,1);
			}
		}
		//更新计数
		$where = 'id= ' . $id .'';
		$DB->do_update('xindmtags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urln),
			 ),
			 $where
		);
	}

	function get_comic_pics($content,$pic_url_pre,$pic_file_pre,$current_url = ''){
		//获取总页数
		$tp = explode('共<strong><font color="#FF0000">',$content);
		$tp = $tp[1];
		$tp = explode('</font>',$tp);
		$tp = $tp[0];
		//获取每页连接
		//file_put_contents('E:\web\hf\xindm\test\123.html', $content);exit;
		$res = array();
		for ($i = 1;$i<$tp;$i++){
			$current_pic_page = $current_url . '&page=' . $i;
			//单独进入每页采集
			$content = mb_convert_encoding(file_get_contents($current_pic_page),"UTF-8","GB2312");
			//获取图片连接前缀
			$picurl = explode('../book',$content);
			$picurl = $picurl[1];
			$picurl = explode('" ',$picurl);
			$picurl = $pic_url_pre . $picurl[0];
			$res[$i - 1] = $picurl;
		}
		// 		$picurl = $pic_url_pre . $picurl . $pic_file_pre;
		// 		$res = array($tp,$picurl);
		return $res;
	}
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'xindmtags', array( 'id'			=> $id,
				'lastupdate'				=> $lu,
				'name'					=> $name,
				'count'					=> $count,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count){
		global $DB;
		$DB->do_insert( 'xindmphotos', array(
				'uid'				=> $uid,
				'url'					=> $url,
				'count'					=> $count,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
?>